package com.jingdianjichi.practice.server.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.jingdianjichi.practice.api.common.Result;
import com.jingdianjichi.practice.api.req.*;
import com.jingdianjichi.practice.api.vo.RankVO;
import com.jingdianjichi.practice.api.vo.ReportVO;
import com.jingdianjichi.practice.api.vo.ScoreDetailVO;
import com.jingdianjichi.practice.api.vo.SubjectDetailVO;
import com.jingdianjichi.practice.server.entity.po.PracticeDetailPO;
import com.jingdianjichi.practice.server.service.Impl.PracticeDetailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.security.KeyPair;
import java.util.List;
import java.util.Objects;

/**
 * ClassName:PracticeDetailController
 * Package:com.jingdianjichi.practice.server.controller
 * Description:
 *
 * @Author fst
 * @Create 2025/1/21 11:31
 * @Version 1.0
 */
@RestController
@RequestMapping("/practice/detail")
@Slf4j
public class PracticeDetailController
{

    @Resource
    private PracticeDetailService practiceDetailService;


    @PostMapping("/submit")
    public Result<Boolean> submit(@RequestBody SubmitPracticeDetailReq req)
    {
        try {
            if(log.isInfoEnabled()) {
                log.info("submit req:{}", req);
            }

            Preconditions.checkArgument(!Objects.isNull(req), "req不能为空");
            Preconditions.checkArgument(!Objects.isNull(req.getSetId()), "setId不能为空");
            Preconditions.checkArgument(!Objects.isNull(req.getTimeUse()), "timeUse不能为空");
            Preconditions.checkArgument(!Objects.isNull(req.getSubmitTime()), "submitTime不能为空");

             Result<Boolean> result = practiceDetailService.submit(req);

             return result;

        }catch (Exception e)
        {
            log.info("submit.error:{}",e.getMessage(),e);
            return Result.fail(e.getMessage());
        }
    }

    @PostMapping("submitSubject")
    public Result<Boolean> submitSubject(@RequestBody SubmitSubjectDetailReq submitSubjectDetailReq)
    {
        try {
            if(log.isInfoEnabled()) {
                log.info("submit req:{}",submitSubjectDetailReq);
            }

            Preconditions.checkArgument(!Objects.isNull(submitSubjectDetailReq), "practiceDetailPO不能为空");
            Preconditions.checkArgument(!Objects.isNull(submitSubjectDetailReq.getPracticeId()), "practiceId不能为空");
            Preconditions.checkArgument(!Objects.isNull(submitSubjectDetailReq.getSubjectId()), "subjectId不能为空");
            Preconditions.checkArgument(!Objects.isNull(submitSubjectDetailReq.getSubjectType()), "subjectType不能为空");

            practiceDetailService.submitSubject(submitSubjectDetailReq);
            return Result.ok();
        }
        catch (Exception e)
        {
            log.info("submit.error:{}",e.getMessage(),e);
            return Result.fail(e.getMessage());
        }
    }

    @PostMapping("/getScoreDetail")
    public Result<List<ScoreDetailVO>> getScoreDetail(@RequestBody GetScoreDetailReq req)
    {
        try {

            if (log.isInfoEnabled())
            {
                log.info("getScoreDetail req:{}", req);
            }

            Preconditions.checkArgument(!Objects.isNull(req), "req不能为空");
            Preconditions.checkArgument(!Objects.isNull(req.getPracticeId()), "practiceId不能为空");

            List<ScoreDetailVO> list = practiceDetailService.getScoreDetail(req);

            if(log.isInfoEnabled())
            {
                log.info("每题得分出参{}", JSON.toJSONString(list));
            }
            return Result.ok(list);
        }
        catch (Exception e)
        {
            log.info("getScoreDetail.error:{}",e.getMessage(),e);
            return Result.fail(e.getMessage());
        }
    }

    @PostMapping("/getSubjectDetail")
    public Result<SubjectDetailVO> getSubjectDetail(@RequestBody GetSubjectDetailReq req)
    {
        try {

            if (log.isInfoEnabled())
            {
                log.info("getPracticeDetail req:{}", req);
            }

            Preconditions.checkArgument(!Objects.isNull(req), "req不能为空");
            Preconditions.checkArgument(!Objects.isNull(req.getPracticeId()), "practiceId不能为空");
            Preconditions.checkArgument(!Objects.isNull(req.getSubjectId()), "subjectId不能为空");
            Preconditions.checkArgument(!Objects.isNull(req.getSubjectType()), "subjectType不能为空");

            SubjectDetailVO subjectDetailVO = practiceDetailService.getSubjectDetail(req);

            if(log.isInfoEnabled())
            {
                log.info("答案详情出参{}",JSON.toJSONString(subjectDetailVO));
            }

            return Result.ok(subjectDetailVO);
        }
        catch (Exception e)
        {

            log.info("getPracticeDetail.error:{}",e.getMessage(),e);
            return Result.fail(e.getMessage());
        }
    }

    @PostMapping("/getReport")
    public Result<ReportVO> getReport(@RequestBody GetReportReq req)
    {
        try {
            if(log.isInfoEnabled()) {
                log.info("获取入参{}",JSON.toJSONString(req));
            }

            Preconditions.checkArgument(!Objects.isNull(req), "req不能为空");
            Preconditions.checkArgument(!Objects.isNull(req.getPracticeId()), "practiceId不能为空");

            ReportVO reportVO = practiceDetailService.getReport(req);

            if(log.isInfoEnabled())
            {
                log.info("评估出参为{}",JSON.toJSONString(reportVO));
            }

            return Result.ok(reportVO);

        }catch (Exception e)
        {
            log.error("getReport.error:{}",e.getMessage(),e);
            return Result.fail(e.getMessage());
        }
    }

    @PostMapping("/getPracticeRankList")
    public Result<List<RankVO>> getPracticeRankList()
    {
        try {
            List<RankVO> list = practiceDetailService.getPracticeRankList();
            if(log.isInfoEnabled())
            {
                log.info("获取排行榜出参{}",JSON.toJSONString(list));
            }
            return Result.ok(list);
        }catch (Exception e)
        {
            log.error("getPracticeRankList.error:{}",e.getMessage(),e);
            return Result.fail(e.getMessage());
        }
    }

    @PostMapping("/giveUp")
    public Result<Boolean> giveUp(@RequestParam("practiceId") Long practiceId)
    {
        try {
            log.info("giveUp practiceId:{}",practiceId);
            Preconditions.checkArgument(!Objects.isNull(practiceId),"practiceId不能为空");
            practiceDetailService.giveUp(practiceId);

            return Result.ok();
        }catch (Exception e)
        {
            log.error("giveUp.error:{}",e.getMessage(),e);
            return Result.fail(e.getMessage());
        }
    }
}
